﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace SynoManager.DownloadStation
{
    internal class ListViewItemSorter : IComparer
    {
        private SortColumn column = SortColumn.Name;
        private bool descending = false;

        public SortColumn Column
        {
            get { return column; }
            set { column = value; }
        }

        public bool Descending
        {
            get { return descending; }
            set { descending = value; }
        }

        public int Compare(object x, object y)
        {
            ListViewItem lvx = (ListViewItem)x;
            ListViewItem lvy = (ListViewItem)y;

            if (lvx.Tag == null)
            {
                if (lvy.Tag == null)
                    return 0;
                else
                    return 1;
            }
            else if (lvy.Tag == null)
                return -1;

            DownloadTask tx = ((DownloadGuiTab.DownloadListItemTag) lvx.Tag).Task;
            DownloadTask ty = ((DownloadGuiTab.DownloadListItemTag) lvy.Tag).Task;

            int multiplier = (descending) ? -1 : 1;

            switch (column)
            {
                case SortColumn.Name:
                    return multiplier * tx.Filename.CompareTo(ty.Filename);
                case SortColumn.State:
                    return multiplier * tx.State.CompareTo(ty.State);
                case SortColumn.ProgressPercent:
                case SortColumn.Progress:
                    return multiplier * tx.Progress.CompareTo(ty.Progress);
                case SortColumn.RateDown:
                    return multiplier * tx.RateCurrent.CompareTo(ty.RateCurrent);
                case SortColumn.RateUp:
                    return multiplier * tx.RateUpload.CompareTo(ty.RateUpload);
                case SortColumn.PeersConnected:
                    return multiplier * tx.PeersConnected.CompareTo(ty.PeersConnected);
                case SortColumn.User:
                    return multiplier * tx.Username.CompareTo(ty.Username);
                case SortColumn.SizeCurrent:
                    return multiplier * tx.SizeCurrent.CompareTo(ty.SizeCurrent);
                case SortColumn.SizeTotal:
                    return multiplier * tx.SizeTotal.CompareTo(ty.SizeTotal);
                case SortColumn.Eta:
                    return multiplier*tx.RemainingEstimate.CompareTo(ty.RemainingEstimate);
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        public enum SortColumn
        {
            // Value should match column index
            Name = 0,
            State = 1,
            Progress = 2,
            ProgressPercent = 3,
            RateDown = 4,
            RateUp = 5,
            PeersConnected = 6,
            User = 7,
            SizeCurrent = 8,
            SizeTotal = 9,
            Eta = 10,
        }
    }
}
